package com.honeyms.project.system.user.controller;

import java.util.List;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.honeyms.common.utils.CookieUtils;
import com.honeyms.common.utils.ServletUtils;
import com.honeyms.common.utils.StringUtils;
import com.honeyms.framework.config.HoneyMSConfig;
import com.honeyms.framework.web.controller.BaseController;
import com.honeyms.project.blog.blogcomment.domain.BlogCommentOpen;
import com.honeyms.project.blog.blogcomment.service.IBlogCommentService;
import com.honeyms.project.blog.blogcontent.domain.BlogContent;
import com.honeyms.project.blog.blogcontent.service.IBlogContentService;
import com.honeyms.project.blog.blognotice.domain.BlogNotice;
import com.honeyms.project.blog.blognotice.service.IBlogNoticeService;
import com.honeyms.project.system.config.service.IConfigService;
import com.honeyms.project.system.menu.domain.Menu;
import com.honeyms.project.system.menu.service.IMenuService;
import com.honeyms.project.system.user.domain.User;
import com.honeyms.project.system.usernotice.domain.MyNotice;
import com.honeyms.project.system.usernotice.service.IUserNoticeService;

/**
 * 首页 业务处理
 * 
 * @author honeyms
 */
@Controller
public class IndexController extends BaseController
{
    @Autowired
    private IMenuService menuService;

    @Autowired
    private IConfigService configService;

    @Autowired
    private HoneyMSConfig honeymsConfig;

    @Autowired
    private IBlogNoticeService blogNoticeService;
    
    @Autowired
    private IBlogContentService blogContentService;
    
    @Autowired
    private IBlogCommentService blogCommentService;
    
    @Autowired
    private IUserNoticeService userNoticeService ;

	@GetMapping({"/blog","","/"})
	String blog(Model model,ModelMap map) {
		if (getSysUser()!=null) {
			model.addAttribute("user",getSysUser());
		}
		BlogNotice blogNotice = new BlogNotice();
		blogNotice.setStatus(0);
		map.put("notices", blogNoticeService.selectBlogNoticeList(blogNotice));
		//加载热门帖子
		BlogContent blogContent = new BlogContent();
		blogContent.setIshot(2);
		map.put("hots", blogContentService.selectBlogContent(blogContent));
		String theme=configService.selectConfigByKey("sys.blog.theme");
		if(theme.equals("main")) {
			//加载最近留言
			startPage(1, 6, "create_date desc");
			List<BlogCommentOpen> list =blogCommentService.selectBlogCommentOpenList(null);
			//map.put("rows", getDataTable(list).getRows());
			map.put("comments", list);
		}
		return "blog/index/"+theme;
	}
    
    // 系统首页
	@RequiresPermissions("admin:blog:blog")
    @GetMapping("/index")
    public String index(ModelMap mmap)
    {
        // 取身份信息
        User user = getSysUser();
        // 根据用户id取出菜单
        List<Menu> menus = menuService.selectMenusByUser(user);
        mmap.put("menus", menus);
        mmap.put("user", user);
        mmap.put("sideTheme", configService.selectConfigByKey("sys.index.sideTheme"));
        mmap.put("skinName", configService.selectConfigByKey("sys.index.skinName"));
        mmap.put("ignoreFooter", configService.selectConfigByKey("sys.index.ignoreFooter"));
        mmap.put("copyrightYear", honeymsConfig.getCopyrightYear());
        mmap.put("demoEnabled", honeymsConfig.isDemoEnabled());
        
        MyNotice myNotice = new MyNotice();
    	myNotice.setUserId(getUserId());
    	myNotice.setIsRead(0);
    	//查询当前用户未读消息
        mmap.put("total", userNoticeService.selectMyNoticeList(myNotice).size());

        // 菜单导航显示风格
        String menuStyle = configService.selectConfigByKey("sys.index.menuStyle");
        // 移动端，默认使左侧导航菜单，否则取默认配置
        String indexStyle = ServletUtils.checkAgentIsMobile(ServletUtils.getRequest().getHeader("User-Agent")) ? "index" : menuStyle;
        
        // 优先Cookie配置导航菜单
        Cookie[] cookies = ServletUtils.getRequest().getCookies();
        for (Cookie cookie : cookies)
        {
            if (StringUtils.isNotEmpty(cookie.getName()) && "nav-style".equalsIgnoreCase(cookie.getName()))
            {
                indexStyle = cookie.getValue();
                break;
            }
        }
        String webIndex = "topnav".equalsIgnoreCase(indexStyle) ? "index-topnav" : "index";
        return webIndex;
    }

    // 切换主题
    @GetMapping("/system/switchSkin")
    public String switchSkin()
    {
        return "skin";
    }

    // 切换菜单
    @GetMapping("/system/menuStyle/{style}")
    public void menuStyle(@PathVariable String style, HttpServletResponse response)
    {
        CookieUtils.setCookie(response, "nav-style", style);
    }

    // 系统介绍
    @GetMapping("/system/main")
    public String main(ModelMap mmap)
    {
    	String ccount=configService.selectConfigByKey("sys.blog.commentcount");
    	if ("".equals(ccount)) {
			ccount="0:0:0:0:0";
		}
    	String[] commentcount = ccount.split(":");
    	int tdaycomment=Integer.parseInt(commentcount[0]);
    	int ydaycomment=Integer.parseInt(commentcount[1]);
    	int qdaycomment=Integer.parseInt(commentcount[2]);
    	boolean tcommentisup = (ydaycomment-tdaycomment)>0;
    	boolean ycommentisup = (qdaycomment-ydaycomment)>0;
    	int qday=Integer.parseInt(commentcount[3]);//最近7七天
    	int tday=Integer.parseInt(commentcount[4]);//最近31天
    	
    	mmap.put("tdaycomment", tdaycomment);
    	mmap.put("tcommentisup", tcommentisup);
    	mmap.put("ydaycomment", ydaycomment);
    	mmap.put("ycommentisup", ycommentisup);
    	mmap.put("qday", qday);
    	mmap.put("tday", tday);
    	
    	
    	String ccount1=configService.selectConfigByKey("sys.blog.contentcount");
    	if ("".equals(ccount1)) {
    		ccount1="0:0:0:0:0";
    	}
    	String[] contentcount = ccount1.split(":");
    	int tdaycontent=Integer.parseInt(contentcount[0]);
    	int ydaycontent=Integer.parseInt(contentcount[1]);
    	int qdaycontent=Integer.parseInt(contentcount[2]);
    	boolean tcontentisup = (ydaycontent-tdaycontent)>0;
    	boolean ycontentisup = (qdaycontent-ydaycontent)>0;
    	int qday1=Integer.parseInt(contentcount[3]);//最近7七天
    	int tday1=Integer.parseInt(contentcount[4]);//最近31天
    	
    	mmap.put("tdaycontent", tdaycontent);
    	mmap.put("tcontentisup", tcontentisup);
    	mmap.put("ydaycontent", ydaycontent);
    	mmap.put("ycontentisup", ycontentisup);
    	mmap.put("qday1", qday1);
    	mmap.put("tday1", tday1);
    	
        mmap.put("version", honeymsConfig.getVersion());
        return "main";
    }
    
    // 游戏大厅
    @GetMapping("/game/gamelobby")
    public String gamelobby(ModelMap mmap)
    {
    	mmap.put("user", getSysUser());
    	return "game/gamelobby";
    }
}
